package com.itheima.d4_algorithm;

import java.util.Arrays;

/**
 * 目标：掌握二分查找算法。
 * <p>
 * binarySearch
 */
public class Test3 {
    public static void main(String[] args) {
        int[] arr = {10, 18, 23, 33, 37, 45, 66, 83};

        System.out.println(efcz(arr, 33));
    }

    public static int efcz(int[] arr, int num) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int min = (left + right) / 2;
            if (arr[min] > num) {
                right = min - 1;
            } else if (arr[min] < num) {
                left = min + 1;
            } else {
                return min;
            }
        }
        return -1;
    }
}
